package com.cloudkinto.controller.order.outbound;


import com.cloudkinto.base.BaseController;
import com.cloudkinto.bo.common.BatchQueryBo;
import com.cloudkinto.bo.order.outbound.*;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.service.order.outbound.OutboundImportExportService;
import com.cloudkinto.service.order.outbound.OutboundOldExportService;
import com.cloudkinto.service.order.outbound.OutboundService;
import com.cloudkinto.vo.common.CommonBatchOperationVo;
import com.cloudkinto.vo.order.outbound.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 出库单 前端控制器
 * </p>
 *
 * @author long
 * @since 2024-11-21
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "Wms出库单模块")
@RequestMapping("/api/admin/wms/order/outbound")
public class OrderOutboundWmsController extends BaseController {
    private final OutboundService outboundService;

    private final OutboundImportExportService outboundImportExportService;

    private final OutboundOldExportService outboundOldExportService;

    @UserSessionRequired
    @PutMapping("/normalUpdate")
    @ApiOperation(value = "一件代发出库单修改", notes = "一件代发出库单修改")
    public SingleResult normalUpdate(@RequestBody @Validated NormalOutboundUpdateBo req) {
        return SingleResult.success("修改成功", outboundService.normalUpdate(req, getCurrentUserId(), getUserName()));
    }

    @UserSessionRequired
    @DeleteMapping("/{id}")
    @ApiOperation(value = "出库单根据id删除", notes = "出库单根据id删除")
    public SingleResult delete(@PathVariable("id") Long id) {
        outboundService.delete(id, getCurrentUserId());
        return SingleResult.success("删除成功");
    }

    @UserSessionRequired
    @GetMapping("/normalOutboundDetail/{id}")
    @ApiOperation(value = "一件代发查看详情", notes = "一件代发查看详情")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = NormalOutboundDetail.class))
    public SingleResult normalOutboundDetail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", outboundService.normalOutboundDetail(id, getCurrentUserId(), "wms"));
    }

    @UserSessionRequired
    @GetMapping("/transferOutboundDetail/{id}")
    @ApiOperation(value = "批发单详情")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = TransferOutboundDetail.class))
    public SingleResult transferOutboundDetail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", outboundService.transferOutboundDetail(id, getCurrentUserId(), "wms"));
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "出库单查询初始化", notes = "出库单查询初始化")
    public SingleResult<Map<String, Object>> pageInit() {
        return outboundService.pageInit(getCurrentUserId(), getTenantId(), getCompanyId(), "wms");
    }

    @UserSessionRequired
    @PostMapping("/getNormalListPage")
    @ApiOperation(value = "一件代发出库单查询（分页）", notes = "一件代发出库单查询（分页）")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = NormalOutboundVo.class))
    public PageResult<NormalOutboundVo> getNormalListPage(@RequestBody @Validated NormalOrderOutboundQueryBo bo) {
        bo.setTenantId(getTenantId());
        bo.setUserId(getCurrentUserId());
        bo.setSource("wms");
        bo.setWhId(getWarehouseId());
        bo.setExceptionSource(1);
        return outboundService.getNormalListPage(bo);
    }

    @UserSessionRequired
    @PostMapping("/getNormalStatusSta")
    @ApiOperation(value = "一件代发状态统计", notes = "一件代发状态统计")
    public SingleResult getNormalStatusSta(@RequestBody @Validated NormalOrderOutboundQueryBo bo) {
        bo.setTenantId(getTenantId());
        bo.setUserId(getCurrentUserId());
        bo.setSource("wms");
        bo.setWhId(getWarehouseId());
        bo.setExceptionSource(1);
        return outboundService.getNormalStatusSta(bo);
    }

    @UserSessionRequired
    @PostMapping("/normalCreateWave")
    @ApiOperation(value = "一件代发生成波次", notes = "一件代发生成波次")
    public SingleResult normalCreateWave(@RequestBody @Validated NormalOutboundCreateWaveBo bo) {
        return outboundService.normalCreateWave(bo, getCurrentUserId(), getUserName());
    }

    @UserSessionRequired
    @GetMapping("/normalWeighDetail/{scan}")
    @ApiOperation(value = "一件代发/批发单查看称重详情", notes = "查看称重详情")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = NormalWeighDetail.class))
    public SingleResult normalWeighDetail(@PathVariable("scan") String scan) {
        return outboundService.normalWeighDetail(scan);
    }

    @UserSessionRequired
    @PostMapping("/weigh")
    @ApiOperation(value = "一件代发/批发单称重(复核、称重页)")
    public SingleResult weigh(@RequestBody @Validated NormalWeighBo bo) {
        return outboundService.weigh(bo, getCurrentUserId(), getUserName(), getWarehouseId());
    }

    @UserSessionRequired
    @PutMapping("/normalQuickOutbound")
    @ApiOperation(value = "一件代发快速出库", notes = "一件代发快速出库")
    public SingleResult normalQuickOutbound(Long[] ids) {
        return outboundService.normalQuickOutbound(Arrays.asList(ids), getCurrentUserId(), getUserName());
    }

    @UserSessionRequired
    @PutMapping("/normalOutboundOperate")
    @ApiOperation(value = "一件代发出库", notes = "一件代发出库")
    public SingleResult normalOutboundBatch(@RequestParam("ids") Long[] ids) {//todo
        return outboundService.normalOutboundOperate(Arrays.asList(ids), getCurrentUserId(), getUserName());
    }

    @UserSessionRequired
    @PutMapping("/markException/{id}")
    @ApiOperation(value = "标记异常", notes = "标记异常")
    public SingleResult markException(@PathVariable("id") Long id) {
        return outboundService.markException(id, getCurrentUserId(), getUserName());
    }

    @UserSessionRequired
    @PostMapping("/exceptionOrderPageList")
    @ApiOperation(value = "异常件", notes = "异常件")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = ExceptionOutboundVo.class))
    public PageResult exceptionOrderPageList(@RequestBody @Validated ExceptionOutboundQueryBo bo) {
        bo.setTenantId(getTenantId());
        return outboundService.exceptionOrderPageList(bo);
    }

    @UserSessionRequired
    @PutMapping("/removeException/{id}")
    @ApiOperation(value = "移除异常", notes = "移除异常")
    public SingleResult removeException(@PathVariable("id") Long id) {
        return outboundService.removeException(id, getCurrentUserId(), getUserName());
    }

    @UserSessionRequired
    @PutMapping("/exceptionConfirm")
    @ApiOperation(value = "确认异常", notes = "确认异常")
    public SingleResult exceptionConfirm(@RequestBody @Validated ExceptionConfirmBo bo) {
        return outboundService.exceptionConfirm(bo, getCurrentUserId(), getUserName());
    }

    @UserSessionRequired
    @PostMapping("/getTransferListPage")
    @ApiOperation(value = "备货中转出库单查询", notes = "备货中转出库单查询")
    public PageResult<TransferOutboundVo> getTransferListPage(@RequestBody @Validated TransferOrderOutboundQueryBo bo) {
        bo.setWhId(getWarehouseId());
        bo.setTenantId(getTenantId());
        bo.setUserId(getCurrentUserId());
        bo.setSource("wms");
        bo.setExceptionSource(1);
        return outboundService.getTransferListPage(bo);
    }

    @UserSessionRequired
    @PostMapping("/getTransferStatusSta")
    @ApiOperation(value = "备货中转状态统计", notes = "备货中转状态统计")
    public SingleResult getTransferStatusSta(@RequestBody @Validated TransferOrderOutboundQueryBo bo) {
        bo.setWhId(getWarehouseId());
        bo.setTenantId(getTenantId());
        bo.setUserId(getCurrentUserId());
        bo.setSource("wms");
        bo.setExceptionSource(1);
        return outboundService.getTransferStatusSta(bo);
    }

    @UserSessionRequired
    @PutMapping("/transferSubmitPick")
    @ApiOperation(value = "备货中转提交拣货", notes = "备货中转提交拣货")
    public SingleResult transferSubmitPick(@RequestParam("ids") Long[] ids) {
        return outboundService.transferSubmitPick(Arrays.asList(ids), getCurrentUserId(), getUserName());
    }

    @UserSessionRequired
    @GetMapping("/transferPickingDetail/{id}")
    @ApiOperation(value = "备货中转拣货详情", notes = "备货中转拣货详情")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = TransferOutboundPickingDetail.class))
    public SingleResult transferPickingDetail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", outboundService.transferPickingDetail(id));
    }

    @UserSessionRequired
    @GetMapping("/transferPickingDetailInit")
    @ApiOperation(value = "备货中转拣货详情初始化", notes = "备货中转拣货详情初始化")
    public SingleResult transferPickingDetailInit() {
        return SingleResult.success("查询成功", outboundService.transferPickingDetailInit(getCurrentUserId(), getTenantId(), "wms"));
    }

    @UserSessionRequired
    @PostMapping("/transferPicking")
    @ApiOperation(value = "备货中转拣货", notes = "备货中转拣货")
    @ApiResponses(@ApiResponse(code = 200, message = "true 完全拣货 false 部分拣货"))
    public SingleResult transferPicking(@RequestBody @Validated TransferOutboundPickingBo bo) {
        return SingleResult.success("拣货成功",
                outboundService.transferPicking(bo, getCurrentUserId(), getUserName(), getTenantId()));
    }

    @UserSessionRequired
    @PostMapping("/transferPickingDetailBatch")
    @ApiOperation(value = "批发单批量拣货详情")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = TransferPickingByBatchDetail.class, responseContainer = "List"))
    public SingleResult transferPickingDetailBatch(@RequestBody BatchQueryBo bo) {
        return SingleResult.success("查询成功", outboundService.transferPickingDetailBatch(bo.getIds()));
    }

    @UserSessionRequired
    @PostMapping("/transferPickingBatch")
    @ApiOperation(value = "批发单批量拣货")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = Long.class, responseContainer = "List"))
    public SingleResult transferPickingBatch(@RequestBody @Validated TransferPickingBatchBo bo) {
        return SingleResult.success("拣货成功",
                outboundService.transferPickingBatch(bo, getCurrentUserId(), getUserName(), getTenantId()));
    }

    @UserSessionRequired
    @GetMapping("/transferOutboundPreOperation")
    @ApiOperation(value = "批发出库前置操作(批量/单个)")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = OutboundPreOperationVo.class))
    public SingleResult transferOutboundPreOperation(@RequestParam("ids") Long[] ids) {
        return SingleResult.success(outboundService.transferOutboundPreOperation(Arrays.asList(ids)));
    }

    @UserSessionRequired
    @PutMapping("/transferOutboundOperationBatch")
    @ApiOperation(value = "批发出库操作(批量)")
    public SingleResult transferOutboundOperationBatch(Long[] ids) {
        return outboundService.transferOutboundOperationBatch(Arrays.asList(ids), getCurrentUserId(), getUserName());
    }

    @UserSessionRequired
    @PutMapping("/transferOutboundOperation")
    @ApiOperation(value = "批发出库操作（单个）")
    public SingleResult transferOutboundOperation(@RequestBody TransferOutboundOperationBo bo) {
        return outboundService.transferOutboundOperation(bo, getCurrentUserId(), getUserName());
    }

    @UserSessionRequired
    @GetMapping("transferOutboundPackDetail/{id}")
    @ApiOperation(value = "中转出库查看装箱详情", notes = "中转出库查看装箱详情")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = TransferOutboundPackDetail.class))
    public SingleResult transferOutboundPackDetail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", outboundService.transferOutboundPackDetail(id));
    }

    @UserSessionRequired
    @PostMapping("/transferOutboundPack")
    @ApiOperation(value = "批发单出库修改装箱信息")
    public SingleResult transferOutboundPack(@RequestBody @Validated TransferOutboundPackBo bo) {
        return outboundService.transferOutboundPack(bo, getCurrentUserId(), getUserName());
    }

    @UserSessionRequired
    @PostMapping("/outboundUpdateChannelAndTrackingNo")
    @ApiOperation(value = "出库单修改物流渠道和跟踪号")
    public SingleResult outboundUpdateChannelAndTrackingNo(@RequestBody @Validated OutboundUpdateChannelAndTrackingNoBo bo) {
        return outboundService.outboundUpdateChannelAndTrackingNo(bo, getCurrentUserId(), getUserName());
    }

    @UserSessionRequired
    @GetMapping("/printNormalShippingList/{id}")
    @ApiOperation(value = "打印一件代发发货清单", notes = "打印一件代发发货清单")
    public void printNormalShippingList(@PathVariable("id") Long id, HttpServletResponse response) throws Exception {
        outboundImportExportService.printNormalShippingList(id, response);
    }

    @UserSessionRequired
    @GetMapping("/printTransferShippingList/{id}")
    @ApiOperation(value = "打印中转发货清单", notes = "打印中转发货清单")
    public void printTransferShippingList(@PathVariable("id") Long id, HttpServletResponse response) throws Exception {
        outboundImportExportService.printTransferShippingList(id, response);
    }

    @UserSessionRequired
    @PutMapping("/uploadAttachment")
    @ApiOperation(value = "上传附件", notes = "上传附件")
    public SingleResult uploadAttachment(@RequestBody @Validated OutboundUploadAttachmentBo bo) {
        return outboundService.uploadAttachment(bo, getCurrentUserId(), getUserName());
    }

    @UserSessionRequired
    @PostMapping("/printExpressSheet")
    @ApiOperation(value = "打印快递单", notes = "打印快递单")
    public void printExpressSheet(@RequestBody @Validated PrintExpressSheetBo bo, HttpServletResponse response) throws Exception {
        outboundImportExportService.printExpressSheet(bo, response);
    }

    @UserSessionRequired
    @GetMapping("/printTransferPickingList/{id}")
    @ApiOperation(value = "批发单打印拣货清单")
    public void printTransferPickingList(@PathVariable("id") Long id, HttpServletResponse response) throws Exception {
        outboundImportExportService.printTransferPickingList(id, response);
    }

    @UserSessionRequired
    @PostMapping("/printTransferPickingListBatch")
    @ApiOperation(value = "批发单打印拣货清单-批量")
    public void printTransferPickingListBatch(@RequestBody BatchQueryBo bo, HttpServletResponse response) throws Exception {
        outboundImportExportService.printTransferPickingListBatch(bo.getIds(), response);
    }

    @UserSessionRequired
    @GetMapping("/getPickerList")
    @ApiOperation(value = "获取拣货人列表", notes = "获取拣货人列表")
    public SingleResult getPickerList() {
        return outboundService.getPickerList(getTenantId());
    }

    @UserSessionRequired
    @PutMapping("/assignLogistics")
    @ApiOperation(value = "指派物流", notes = "指派物流")
    public SingleResult assignLogistics(@RequestBody @Validated AssignLogisticsBo bo) {
        return outboundService.assignLogistics(bo, getCurrentUserId(), getUserName());
    }

    @UserSessionRequired
    @GetMapping("/getImportUpdateTrackingNumberTemplate")
    @ApiOperation(value = "获取导入更新跟踪号模板", notes = "获取导入更新跟踪号模板")
    public void importUpdateTrackingNumberTemplate(HttpServletResponse response) throws IOException {
        outboundImportExportService.getImportUpdateTrackingNumberTemplate(response);
    }

    @UserSessionRequired
    @PutMapping("importUpdateTrackingNumber")
    @ApiOperation(value = "导入更新跟踪号", notes = "导入更新跟踪号")
    @ApiResponses(@ApiResponse(code = 200, message = "操作成功", response = CommonBatchOperationVo.class))
    public SingleResult importUpdateTrackingNumber(MultipartFile file) {
        return SingleResult.success(outboundImportExportService.importUpdateTrackingNumber(file, getTenantId(), getCurrentUserId(), getUserName(), getWarehouseId()));
    }

    @UserSessionRequired
    @PutMapping("importUpdatePackage")
    @ApiOperation(value = "导入更新包裹", notes = "导入更新包裹")
    @ApiResponses(@ApiResponse(code = 200, message = "操作成功", response = CommonBatchOperationVo.class))
    public SingleResult importUpdatePackage(MultipartFile file) {
        return SingleResult.success(outboundImportExportService.importUpdatePackage(file, getTenantId(), getCurrentUserId(), getUserName()));
    }

    @UserSessionRequired
    @PostMapping("/normalOutboundExport")
    @ApiOperation(value = "一件代发导出wms", notes = "一件代发导出")
    public void normalOutboundExport(@RequestBody @Validated NormalOrderOutboundQueryBo bo, HttpServletResponse response) throws IOException {
        bo.setTenantId(getTenantId());
        bo.setUserId(getCurrentUserId());
        bo.setSource("wms");
        bo.setWhId(getWarehouseId());
        bo.setExceptionSource(1);
        outboundImportExportService.normalOutboundExportWmsOmp(bo, getWarehouseId(), null, response);
    }

    @UserSessionRequired
    @PostMapping("/normalOutboundExportBySimple")
    @ApiOperation(value = "一件代发导出简单")
    public void normalOutboundExportBySimple(@RequestBody @Validated NormalOrderOutboundQueryBo bo, HttpServletResponse response) throws IOException {
        bo.setTenantId(getTenantId());
        bo.setUserId(getCurrentUserId());
        bo.setSource("wms");
        bo.setWhId(getWarehouseId());
        bo.setExceptionSource(1);
        outboundImportExportService.normalOutboundExportBySimple(bo, response);
    }

    @UserSessionRequired
    @PostMapping("/transferOutboundExportBySimple")
    @ApiOperation(value = "批发单导出简单")
    public void transferOutboundExportBySimple(@RequestBody @Validated TransferOrderOutboundQueryBo bo, HttpServletResponse response) throws IOException {
        bo.setWhId(getWarehouseId());
        bo.setTenantId(getTenantId());
        bo.setUserId(getCurrentUserId());
        bo.setSource("wms");
        bo.setExceptionSource(1);
        outboundImportExportService.transferOutboundExportBySimple(bo, response);
    }

    @UserSessionRequired
    @PostMapping("/transferOutboundExport")
    @ApiOperation(value = "备货中转导出wms", notes = "备货中转导出wms")
    public void transferOutboundExport(@RequestBody @Validated TransferOrderOutboundQueryBo bo, HttpServletResponse response) throws IOException {
        bo.setWhId(getWarehouseId());
        bo.setTenantId(getTenantId());
        bo.setUserId(getCurrentUserId());
        bo.setSource("wms");
        bo.setExceptionSource(1);
        outboundImportExportService.transferOutboundExportWmsOmp(bo, getWarehouseId(), null, response);
    }

    @UserSessionRequired
    @PostMapping("/oldExport")
    @ApiOperation(value = "一期labella系统导出", notes = "一期labella系统导出")
    public void oldExport(@RequestBody @Validated NormalOrderOutboundQueryBo bo, HttpServletResponse response) throws IOException {
        bo.setTenantId(getTenantId());
        bo.setUserId(getCurrentUserId());
        bo.setSource("wms");
        bo.setWhId(getWarehouseId());
        bo.setExceptionSource(1);
        if (bo.getTemplateType() == 1) {
            outboundOldExportService.exportOrderFileUseZip(bo, response);
        } else if (bo.getTemplateType() == 2) {
            outboundOldExportService.exportOrderFileUseZipV2(bo, response);
        }
    }

    @UserSessionRequired
    @GetMapping("/getDeliveryOrder/{transferId}")
    @ApiOperation(value = "获取批发单的納品書", notes = "获取批发单的納品書")
    public void getDeliveryOrder(@PathVariable Long transferId, HttpServletResponse response) throws IOException {
        outboundImportExportService.getDeliveryOrderZip(transferId, response);
    }

    @UserSessionRequired
    @PostMapping("/getDeliveryOrderBatch")
    @ApiOperation(value = "获取批发单的納品書-批量")
    public void getDeliveryOrderBatch(Long[] ids, HttpServletResponse response) throws IOException {
        outboundImportExportService.getDeliveryOrderBatch(Arrays.asList(ids), response);
    }
}

